import requests
from lxml import etree


def chapter_downloader(url):
    """ 下载指定的章节标题和内容并保存 """
    # 伪装成浏览器
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36'}
    # 获取网页
    response = requests.get(url,headers=headers)
    # 编码
    response.encoding = 'utf-8'
    # 转换
    selector = etree.HTML(response.text)
    # 章节标题
    title = selector.xpath('//h1/text()')[0]
    # 章节内容
    content = selector.xpath('//div[@id="content"]/text()')
    content = ''.join(content)

    # 获取小说名
    name = selector.xpath('//title/text()')[0].split('_')[1]

    with open(f'./xiaoshuo/{name}.txt','a',encoding='utf-8') as file:
        file.write(title+'\n'+content+'\n')

# 获取所有章节的地址
def huoquallhref(url):
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36'}
    # 获取网页
    response = requests.get(url,headers=headers)
    # 编码
    response.encoding = 'utf-8'
    # 转换
    selector = etree.HTML(response.text)
    href = selector.xpath('//div[@id="list"]/dl/dd/a/@href') # 第13个标签
    wangzhi = 'https://www.quge6.com'
    newwangzhi = []
    for i in href:
        newwangzhi.append(wangzhi + i)
    newwangzhi = newwangzhi[12:]
    return newwangzhi
def houqubook():
    #https://www.quge6.com/87_87149/
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36'}
    # 获取网页
    response = requests.get('https://www.quge6.com/bqgph.html',headers=headers)
    # 编码
    response.encoding = 'utf-8'
    # 转换
    selector = etree.HTML(response.text)

    hrefs = selector.xpath('//div[@class="topbooks"]/ul/li/a/@href') 
    baseurl='https://www.quge6.com'
    hrefs = [baseurl + i for i in hrefs]
    return hrefs
if __name__ == '__main__':
    bookwangzhi = houqubook()
    for i in bookwangzhi:
        wangzhi = huoquallhref(i)
        for j in wangzhi[:2]:
            chapter_downloader(j)
#    print(houqubook())